package gov.kqqzyyy.monkey.orderpad.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.object.DC_DDXX;


/**
 * Created by Monkey on 2017/9/8.
 * 历史订单适配器
 */

public class OrderListAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private List<DC_DDXX> list;
    private List<DC_DDXX> allList;
    private int filter;

    public OrderListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        filter = 1;
    }

    public void setList(List<DC_DDXX> newList) {
        allList = newList;
        doFilter();
        notifyDataSetChanged();
    }

    public void setFilter(int filter) {
        this.filter = filter;
        doFilter();
        notifyDataSetChanged();
    }

    private void doFilter() {
        if (allList!=null){
            list = new ArrayList<>();
            for (DC_DDXX dcDdxx : allList) {
                String ddh = dcDdxx.getDDH();
                int cc_num = Character.getNumericValue(ddh.charAt(ddh.length() - 2));
                if (cc_num == filter) {
                    list.add(dcDdxx);
                }
            }
            Collections.sort(list, (arg0, arg1) -> arg0.getDDH().compareTo(arg1.getDDH()));
        }
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;

    }

    @Override
    public Object getItem(int i) {
        if (list != null && list.get(i) != null) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_orderhis_detail, parent, false);
            holder.mOrderInfoView = convertView.findViewById(R.id.order_his_textview);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            DC_DDXX dcDdxx = list.get(position);
            if (dcDdxx != null) {
                String ddh = dcDdxx.getDDH();
                int cc_num = Character.getNumericValue(ddh.charAt(ddh.length() - 2));
                String cc_text = "";
                switch (cc_num) {
                    case 1:
                        cc_text = "早餐";
                        break;
                    case 2:
                        cc_text = "午餐";
                        break;
                    case 3:
                        cc_text = "晚餐";
                        break;
                }
                String ddzt_num = dcDdxx.getDDZT();
                String ddzt_text = "";
                switch (ddzt_num) {
                    case "0":
                        ddzt_text = "未付款";
                        break;
                    case "1":
                        ddzt_text = "已付款";
                        break;
                }
                String date_text = new SimpleDateFormat("MM月dd日", Locale.CHINA).format(new Date());
                String finalText = date_text + cc_text + ddzt_text + ":" + ddh;
                holder.mOrderInfoView.setText(finalText);
            }
        }
        return convertView;
    }

    class ViewHolder {
        private TextView mOrderInfoView;
    }
}
